/**
 * Copyright 2011 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package saig.engine.utils;

import java.awt.Rectangle;

import saig.engine.models.Model;

/**
 * Collision Utility class.
 * @author Adam
 * @version $Id$
 * 
 * Created on May 26, 2011 at 7:52:36 PM 
 */
public final class CollisionUtil {
	public enum Location {
		TOP, LEFT, RIGHT, BOTTOM, FULL, NONE
	}
	/**
	 * Private constructor.
	 */
	private CollisionUtil() { }
	/**
	 * Tests model1 against model2 and determines if a collision happens, then returns the location of the collision.
	 * @param model1 The first model.
	 * @param model2 The second model.
	 * @return Location.
	 */
	public static Location getCollision(final Model model1, final Model model2) {
		final Rectangle m1 = model1.getBoundingBox();
		final Rectangle m2 = model2.getBoundingBox();
		// Check the simple case first.
		if (!m1.intersects(m2)) {
			return Location.NONE;
		}
		int valX = 0;
		if (m2.x >= (m1.x +(m1.width / 2)) && m2.x <= m1.x + m1.width) {
			valX = 0; // Right side
		} else if ((m2.x + m2.width) < (m1.x + (m1.width / 2)) && (m2.x + m2.width) >= m1.x) {
			valX = 1; // Left side
		} else {
			valX = 2; // H Center
		}
		int valY = 0;
		if (m2.y >= (m1.y + (m1.height / 2)) && m2.y <=  (m1.y + m1.height)) {
			valY = 0; // Bottom
		} else if ((m2.y + m2.height) > m1.y && (m2.y + m2.height) <= (m1.y + (m1.height / 2))) {
			valY = 1; // Top
		} else {
			valY = 2; // V Center
		}
		Location loc = Location.FULL;
		if (valY == 2) {
			if (valX == 1) {
				loc = Location.LEFT;
			} else if (valX == 0){
				loc = Location.RIGHT;
			}
		} else {
			if (valY == 1) {
				loc = Location.TOP;
			} else if (valY == 0) {
				loc = Location.BOTTOM;
			}
		}
		return loc;
	}
}
